package com.leven.easyerp.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.leven.easyerp.util.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Action基类
 * 
 * @author Leven.Chen
 */
public abstract class BaseAction extends ActionSupport {
	private static final long serialVersionUID = -942753739832701583L;

	protected HttpServletRequest getRequest() {
		return Struts2Utils.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return Struts2Utils.getResponse();
	}

	protected HttpSession getHttpSession() {
		return Struts2Utils.getSession();
	}

	/**
	 * 获取String类型参数值
	 */
	protected String getString(String paramName, String defaultValue) {
		String value = getRequest().getParameter(paramName);
		if (StringUtils.isBlank(value)) {
			return defaultValue;
		} else {
			return value.trim();
		}
	}

	/**
	 * 获取Integer类型参数值
	 */
	protected Integer getInt(String paramName, Integer defaultValue) {
		String value = getString(paramName, null);
		if (value != null) {
			try {
				return Integer.valueOf(value.trim());
			} catch (Exception e) {
			}
		}
		return defaultValue;
	}

	/**
	 * 获取Long类型参数值
	 */
	protected Long getLong(String paramName, Long defaultValue) {
		String value = getString(paramName, null);
		if (value != null) {
			try {
				return Long.valueOf(value.trim());
			} catch (Exception e) {
			}
		}
		return defaultValue;
	}

	/**
	 * 获取Boolean类型参数值
	 */
	protected Boolean getBoolean(String paramName, Boolean defaultValue) {
		String value = getString(paramName, null);
		if (value != null) {
			try {
				return Integer.valueOf(value) != 0;
			} catch (Exception e) {
			}
			try {
				return Boolean.valueOf(value);
			} catch (Exception e) {
			}
		}
		return defaultValue;
	}
}
